package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * 员工管理
 */

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    /**
     * 登录
     *
     * @param employee
     * @param req
     * @return
     */

    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest req) {
        //获取前端提交的密码md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //通过用户名查询对应的数据
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(lqw);
        //判断数据是否存在
        if (emp == null) {
            return R.error("用户名不存在!");
        }
        //判断密码是否相等
        if (!emp.getPassword().equals(password)) {
            return R.error("密码不正确");
        }
        //判断是否禁用
        if (emp.getStatus() == 0) {
            return R.error("该账号已被禁用，无法登录");
        }
        //给Session存入数据
        req.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /**
     * 退出登录
     *
     * @param req
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest req) {
        req.getSession().invalidate();
        return R.success("退出成功");
    }

    /**
     * 添加
     *
     * @param employee
     * @param req
     * @return
     */

    @PostMapping
    public R<String> save(@RequestBody Employee employee, HttpServletRequest req) {
        //设置默认密码并md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
      /*  //设置当前操作时间
        //        employee.setCreateTime(LocalDateTime.now());
        //        //设置当前修改时间
        //        employee.setUpdateTime(LocalDateTime.now());
        //        //设置操作用户id
        //        Long id = (Long) req.getSession().getAttribute("employee");
        //        //设置添加操作id
        //        employee.setCreateUser(id);
        //        //设置修改操作id
        //        employee.setUpdateUser(id);
        //        //执行添加操作*/
        employeeService.save(employee);
        return R.success("添加成功");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */

    @GetMapping("/page")
    public R<Page> PageQuery(int page, int pageSize, String name) {
        log.info("page:{}", page);
        log.info("pageSize:{}", pageSize);
        log.info("name:{}", name);
        //设置分页插件
        Page<Employee> pageHelp = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper();
        //设置模糊查询条件,如果返回假就不添加此条件
        lqw.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        //根据修改时间排序
        lqw.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageHelp, lqw);
        return R.success(pageHelp);
    }

    /**
     * 修改
     *
     * @param employee
     * @param req
     * @return
     */

    @PutMapping
    public R<String> update(@RequestBody Employee employee, HttpServletRequest req) {
        log.info("数据为:{}", employee.toString());
       /* //获取存入session中用户得id
        Long id = (Long) req.getSession().getAttribute("employee");
        //设置更新时间
        employee.setUpdateTime(LocalDateTime.now());
        //设置更新得用户
        employee.setUpdateUser(id);*/
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /**
     * 编辑时数据回显
     *
     * @param id
     * @return
     */

    @GetMapping("{id}")
    public R<Employee> getById(@PathVariable Long id) {
        log.info("获取到得id:{}", id);
        Employee employee = employeeService.getById(id);
        if (employee != null) {
            return R.success(employee);
        }
        return R.error("没有查询到信息!");
    }
}
